package com.sk.commonadapter_android.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * @Author yemao
 * @Email yrmao9893@163.com
 * @Date 2017/7/26
 * @Des 自定义弹性布局!
 */

public class ElasticLayout extends RelativeLayout{
    private View mElasticView;
    private float mLastY;
    private static final float DRAG_RATE = 1.75f;
    public ElasticLayout(Context context) {
        super(context);
        init();
    }

    public ElasticLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ElasticLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
//        measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mElasticView=new View(getContext());
        ViewGroup.LayoutParams mParams=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mElasticView.setLayoutParams(mParams);
        //在父view添加一个子view在顶部，根据动态设置高度来实现，弹性效果
        this.addView(mElasticView);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mLastY=event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                float moveY=event.getRawY()-mLastY;
                mLastY=event.getRawY();
                if (isOnTop()){
                    smoothScrollTo((int) (moveY/DRAG_RATE)+getVisiableHeight());
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
//                smoothScrollTo(0);
                break;
        }
        return super.onTouchEvent(event);
    }
    private boolean isOnTop() {
        if (mElasticView.getParent() != null)
            return true;
        else
            return false;
    }
    private void smoothScrollTo(int height){
        ValueAnimator mValueAnimator= ValueAnimator.ofInt(getVisiableHeight(), height);
        mValueAnimator.setDuration(300);
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                setVisiableHeight((Integer) animation.getAnimatedValue());
            }
        });
        mValueAnimator.start();
    }
    private void setVisiableHeight(int height){
        if (height<0)
            height=0;
        ViewGroup.LayoutParams lp=mElasticView.getLayoutParams();
        lp.height=height;
        mElasticView.setLayoutParams(lp);
    }
    private int getVisiableHeight(){
       return mElasticView.getHeight();
    }
}
